#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void GPGSV_Analyses(char *s1,int s2[]);//s1要解析的字符串数组，s2解析完成后所存放的数组
void BDGSV_Analyses(char *s1,int s2[]);
void GNGGA_Analyses(char *s1,double s2[]);
void GNGSA_Analyses(char *s1,int s2[]);
void GNRMC_Analyses(char *s1,double s2[]);
void GNVTG_Analyses(char *s1,double s2[]);
void GNZDA_Analyses(char *s1,double s2[]);
int main()
{
	char GPGSV1[]="$GPGSV,3,1,12,07,71,308,41,08,71,034,49,11,62,151,25,21,43,133,22*70";
	char GPGSV2[]="$GPGSV,3,2,12,30,36,313,49,27,30,044,43,09,26,219,22,01,25,160,19*7E";
	char GPGSV3[]="$GPGSV,3,3,12,14,18,284,30,28,13,280,25,16,12,079,40,04,09,186,17*74";
	char BDGSV[]="$BDGSV,1,1,02,207,69,026,45,210,67,322,45*65";
	char GNGGA[]="$GNGGA,104630.000,3512.4194,N,10514.7439,E,1,09,1.2,1820.2,M,-40.5,M,,0000*58";
	char GNGSA1[]="$GNGSA,A,3,27,16,30,08,07,14,09,,,,,,1.7,1.2,1.2*28";
	char GNGSA2[]="$GNGSA,A,3,207,210,,,,,,,,,,,1.7,1.2,1.2*2C";
	char GNRMC[]="$GNRMC,104636.800,A,3512.4179,N,10514.7439,E,000.0,182.3,160121,,,A*76";
	char GNVTG[]="$GNVTG,182.3,T,,M,000.0,N,000.0,K,A*1B";
	char GNZDA[]="$GNZDA,104641.800,16,01,2021,00,00*41";

	int GPGSV1_DATA[19];
	int GPGSV2_DATA[19];
	int GPGSV3_DATA[19];

	int BDGSV_DATA[12];

	double GNGGA_DATA[14];

	int GNGSA1_DATA[20];
	int GNGSA2_DATA[20];

	double GNRMC_DATA[12];
	double GNVTG_DATA[8];
	double GNZDA_DATA[7];

//    GPGSV_Analyses(GPGSV1,GPGSV1_DATA);		
//	GPGSV_Analyses(GPGSV2,GPGSV2_DATA);		
//	GPGSV_Analyses(GPGSV3,GPGSV3_DATA);

//	BDGSV_Analyses(BDGSV,BDGSV_DATA);
	
//GNGGA_Analyses(GNGGA,GNGGA_DATA);

//	GNGSA_Analyses(GNGSA1,GNGSA1_DATA);
//	GNGSA_Analyses(GNGSA2,GNGSA2_DATA);
	
//	GNRMC_Analyses(GNRMC,GNRMC_DATA);
	GNVTG_Analyses(GNVTG,GNVTG_DATA);
	GNZDA_Analyses(GNZDA,GNZDA_DATA);

	return 0;
}


void GPGSV_Analyses(char *s1,int s2[])//s1要解析的字符串数组，s2解析完成后所存放的数组
{
	char *ret;
//	int *save;
//	save=s2;
	int i =0;
	ret=strtok(s1,",");
	while((ret=strtok(NULL,","))!=NULL)
		{
	//		printf("ret=%s\n",ret);
			s2[i]=atoi(ret);
			//printf(" %d\n",s2[i]);
			i++;
		}
		printf("---------------GPGSV解析------------------\n");
		printf("GSV语句总数: %d\n",s2[0]);
		printf("本句GSV的编号：%d\n",s2[1]);
		printf("可见卫星总数：%d\n",s2[2]);
		printf("卫星编号：%d\n",s2[3]);
		printf("卫星仰角：%d\n",s2[4]);
		printf("卫星方位角：%d\n",s2[5]);
		printf("信噪比：%d\n",s2[6]);
		
		printf("------------------------------------------\n");

}

void BDGSV_Analyses(char *s1,int s2[])
{
	char *ret;
	int i =0;
	ret=strtok(s1,",");
	while((ret=strtok(NULL,","))!=NULL)
		{
	//		printf("ret=%s\n",ret);
			s2[i]=atoi(ret);
			//printf(" %d\n",s2[i]);
			i++;
		}
		printf("---------------BDGSV解析------------------\n");
		printf("GSV语句总数: %d\n",s2[0]);
		printf("本句GSV的编号：%d\n",s2[1]);
		printf("可见卫星总数：%d\n",s2[2]);
		printf("卫星编号：%d\n",s2[3]);
		printf("卫星仰角：%d\n",s2[4]);
		printf("卫星方位角：%d\n",s2[5]);
		printf("信噪比：%d\n",s2[6]);
		
		printf("------------------------------------------\n");

}
void GNGGA_Analyses(char *s1,double s2[])
{
	char *temp[14];
	char *ret;
	char s3[20];//将*temp[]指针所指内容写入此数组，用于暂存中转
	int i=0;
	ret=strtok(s1,",");
	while((ret=strtok(NULL,","))!=NULL)
	{
		temp[i]=ret;
		printf("%s\n",temp[i]);
		strcpy(s3,temp[i]);
		s2[i]=atof(s3);
	//	printf("装置数据：%lf\n",s2[i]);
		i++;
	}
	printf("---------------GNGGA解析------------------\n");
	printf("UTC时间：%s\n",temp[0]);
	printf("维度： %s %s\n",temp[1],temp[2]);
	printf("经度：%s %s\n",temp[3],temp[4]);
	char tem=*temp[5];
	switch (tem)
	{
		case '0':
				printf("GPS状态：未定位\n");
				break;
		case '1':
				printf("GPS状态：非差分定位\n");
				break;
		case '2':
				printf("GPS状态：差分定位\n");
				break;
	}

	printf("正在使用的用于定位的卫星数量 %s\n",temp[6]);
	printf("HDOP水平准确度因子:%s\n",temp[7]);
	printf("海拔高度：%s\n",temp[8]);
	printf("大地水准面高度：%s\n",temp[10]);
//	printf("差分时间：%s\n",temp[12]);
	printf("差分参考基站标号：%s\n",strtok(temp[12],"*"));
	if(i>13)
	{
		printf("差分时间：%s\n",temp[12]);
	}
	
	printf("--------------------------------------------\n");

}



void GNGSA_Analyses(char *s1,int s2[])
{
	char *temp[20];
	char *ret;
//	char s3[20];//将*temp[]指针所指内容写入此数组，用于暂存中转
	int i=0;
	ret=strtok(s1,",");
	while((ret=strtok(NULL,","))!=NULL)
	{
		temp[i]=ret;
	//	printf("%s\n",temp[i]);
	//	strcpy(s3,temp[i]);
//		printf("截取数据：%s\n",temp[i]);
		i++;
	}

	printf("---------------GNGSA解析------------------\n");
	int	compare=strcmp(temp[0],"A");
	if(compare==0)
	{
		printf("模式：自动\n");
	}
	else
	{
		printf("模式：手动\n");
	}
	int Sattal_Mode=atoi(temp[1]);
	switch(Sattal_Mode)
	{
		case 1:
				printf("定位类型：未定位\n");
				break;
		case 2:
				printf("定位类型：2D定位\n");
				break;
		case 3:
				printf("定位类型：3D定位\n");
				break;
	}
	//i 很关键，用来定位各元素的位置
	i--;
	printf("VDOP垂直精准度因子：%s\n",strtok(temp[i],"*"));
	i--;
	printf("HDOP水平精准度因子：%s\n",temp[i]);
	i--;
	printf("PDOP综合精准度因子：%s\n",temp[i]);

	printf("正在用于定位的卫星号：");
	i--;
	if(i<=0)
	{
		printf("无");
	}
	else
	{
		for(;i>1;i--)
		{
			printf("%s  ",temp[i]);
		}


		printf(" \n");

	}
	printf("--------------------------------------------\n");

}

//用与GNGGA解析的不同方法
void GNRMC_Analyses(char *s1,double s2[])
{
	char *temp[14];
	char *ret;
//	char s3[20];//将*temp[]指针所指内容写入此数组，用于暂存中转
	int i=0;
	ret=strtok(s1,",");
	while((ret=strtok(NULL,","))!=NULL)
	{
		temp[i]=ret;
	//	printf("%s\n",temp[i]);
	//	strcpy(s3,temp[i]);
	//	s2[i]=atof(s3);
	//	printf("装置数据：%lf\n",s2[i]);
		i++;
	}
	
	printf("---------------GNRMC解析------------------\n");
	printf("UTC时间：%s\n",temp[0]);
	printf("定位状态：");
	if(strcmp(temp[1],"A")==0)//判断定位状态是否有效
	{	
		printf("有效\n");
		printf("经度：%s %s\n",temp[2],temp[3]);
		printf("维度：%s %s\n",temp[4],temp[5]);
		printf("地面速度：%s\n",temp[6]);
		printf("地面航向：%s\n",temp[7]);
	//UTC日期字符串拆解 ddmmyy 日月年
		char day[4];
		char month[4];
		char year[4];
		strncpy(day,temp[8],2);
		strncpy(month,temp[8]+2,2);//有问题，不知道为什么
		strncpy(year,temp[8]+4,2);
		printf("UTC日期：%s年%s月%s日\n",year,month,day);
		printf("UTC日期：%s\n",temp[8]);
	//	printf("%d\n",strlen(temp[8]));
	//	printf("%s\n",temp[8]+2);
	//	printf("%s\n",temp[8]+4);
	//	printf("磁偏角：%s\n",temp[9]);
	//	printf("磁偏角方向：%s\n",temp[10]);
		i--;
		char *indix=strtok(temp[i],"*");
		switch (*indix)
		{
			case 'A':
					printf("模式指示：自主定位\n");
					break;
			case 'D':
					printf("模式选择：差分\n");
					break;
			case 'E':
					printf("模式选择：估算\n");
					break;
			case 'N':
					printf("模式选择：数据无效\n");
					break;
		}
	}
	else
	{
		printf("无效\n");
	}
	if(i>=11)
	{
		printf("磁偏角：%s\n",temp[9]);
		printf("磁偏角方向：%s\n",temp[10]);
	}
	else
	{
		printf("磁偏角：无\n");
		printf("磁偏角方向：无\n");
	}

	printf("--------------------------------------------\n");
}

void GNVTG_Analyses(char *s1,double s2[])
{
	char *temp[14];
	char *ret;
	int i=0;
	ret=strtok(s1,",");
	while((ret=strtok(NULL,","))!=NULL)
	{
		temp[i]=ret;
	//	printf("%s\n",temp[i]);
		i++;
	}
	printf("---------------GNVTG解析------------------\n");
	printf("真北地面航向：%s\n",temp[0]);
	if(i>10)
	{
		printf("磁北地面航向：%s\n",temp[2]);
	}	
	else
	{
		printf("磁北地面航向：无\n");
	}
	printf("地面速率：%s节\n",temp[3]);
	printf("地面速率：%s公里/小时\n",temp[5]);
	i--;
	char *indix=strtok(temp[i],"*");
	switch (*indix)
		{
			case 'A':
					printf("模式指示：自主定位\n");
					break;
			case 'D':
					printf("模式选择：差分\n");
					break;
			case 'E':
					printf("模式选择：估算\n");
					break;
			case 'N':
					printf("模式选择：数据无效\n");
					break;
		}
	printf("-----------------------------------------\n");
}

void GNZDA_Analyses(char *s1,double s2[])
{	
	char *temp[14];
	char *ret;
	int i=0;
	ret=strtok(s1,",");
	while((ret=strtok(NULL,","))!=NULL)
	{
		temp[i]=ret;
	//	printf("%s\n",temp[i]);
		i++;
	}
	printf("---------------GNZDA解析------------------\n");
	printf("UTC时间：%s\n",temp[0]);
	printf("%s年%s月%s日\n",temp[3],temp[2],temp[1]);
	printf("本地区域小时：%s\n",temp[4]);
	printf("本地区域分钟：%s\n",strtok(temp[5],"*"));
	
	printf("------------------------------------------\n");
}
